package Navi_run;

import Navi_utils.GeoHash;
import encryption.Hash;
import objects.LatLonPoint;
import objects.LocationBean;
import org.ujmp.core.Matrix;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static Navi_run.Navigation_query.driver_generate_NQ;
import static Navi_run.params.hsvj;
import static Navi_run.params.pidj;
import static encryption.Fuzzy_search.*;
import static encryption.Fuzzy_search.lshMinHash;

public class generate_report {
    public static boolean generate_pidj_hsvj(LocationBean indj, int K, int r3, int te, String daiertaru) {
        try {
            GeoHash geoHash = new GeoHash(indj);
            geoHash.getGeoHashBase32();
            pidj = Hash.sha256_byte(geoHash.getGeoHashBase32() + daiertaru + te);
            hsvj = Hash.sha256_byte("" + K + geoHash.getGeoHashBase32());
            params.indj = indj;
            params.pidjs.add(pidj);
            params.hsvjs.add(hsvj);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static  Map<String, Object> generate_Mj(LocationBean indj, byte[] hsvj, List<byte[]> pidjs, List<byte[]> hsvjs, int K, String daiertaru) {
        try {
            Map<String, Object> Mj = new HashMap<>();
            byte[] bytes = new byte[hsvjs.get(0).length];
            //连续异或所有hsvj
            for (byte[] temp_hsvj : hsvjs) {
                bytes = xor(bytes, temp_hsvj);
            }
            //建立Mj
            Mj.put("indj", indj);
            Mj.put("pidjs", pidjs);
            Mj.put("hsvj", hsvj);
            Mj.put("xor_hsvjs", bytes);
            return Mj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //生成要发送给RSU的报告
    public static Map<String, Object> generate_report( LocationBean locationBean,String te, int rci, BigInteger si) {
        double[][] EncKI = new double[2][(int) TA_driver.driver_V.getColumnCount()];
        byte[] key = null;
        double[] I = BuildIndex(locationBean, key, lshMinHash);
        Map<String, Matrix> EncIndex;
        EncIndex = Enc_Query(TA_driver.SK1, TA_driver.SK2, TA_driver.driver_V, I);
        //把加密结果EncIndex保存到EncKI中
        Matrix SK1_I1 = EncIndex.get("SK1_I1").transpose();
        Matrix SK2_I2 = EncIndex.get("SK2_I2").transpose();
        for (int j = 0; j < SK1_I1.getColumnCount(); j++) {
            EncKI[0][j] = SK1_I1.getAsDouble(0, j);
            EncKI[1][j] = SK2_I2.getAsDouble(0, j);
        }
        Map<String, Object> DR = new HashMap<>();
        DR = driver_generate_NQ("12345678", TA_driver.rci, TA_driver.si, EncKI, true);
        return DR;
    }

    public static byte[] xor(byte[] Byte1_temp, byte[] Byte2_temp) {
        byte[] Byte_temp = new byte[Byte1_temp.length];
        for (int i = 0; i < Byte1_temp.length; i++) {
            Byte_temp[i] = (byte) (Byte1_temp[i] ^ Byte2_temp[i]);
        }
        return Byte_temp;
    }


    public static void main(String[] args) {
        double a, b;
        LocationBean locationBea = new LocationBean(35.666666, 35.666889);
        a = b = 35.3336666;
        SecureRandom secureRandom = new SecureRandom();
        for (int i = 0; i <= 7; i++) {
            LocationBean locationBean = new LocationBean(a, b);
            a += 0.000005;
            b += 0.000010;
            generate_pidj_hsvj(locationBean, secureRandom.nextInt(), secureRandom.nextInt(), 555666, "getreport");
        }
        generate_Mj(locationBea, hsvj, params.pidjs, params.hsvjs, secureRandom.nextInt(), "getreport");
    }
}
